// 导入需要的config和request
import confing from '../config/config.js';
import request from '../util/request.js';
//申明请求对象
const indexRequest = {

    //defect请求的基本url
    BASE_URL: confing.QC_URL + "/index",



    /**
     * 保存自动生成indexCode的值
     * @param {*} index 
     * @param {*} callback 
     */
    generateIndexCode(callback) {

        request.get(this.BASE_URL + "/generate", callback);

    },

    /**
     * 保存常见缺陷信息
     * @param {*} index 
     * @param {*} callback 
     */
    save(index, callback) {
        if (index) {
            request.post(this.BASE_URL + "/save", index, callback);
        }
    },


    /**
        * 编辑缺陷信息
        * @param {*} index 
        * @param {*} callback 
        */
    update(index, callback) {
        if (index != undefined && index.indexCode != undefined) {
            request.post(this.BASE_URL + "/update", index, callback);
        }
    },
    /**
        * 根据indexCode加载常见缺陷信息
        * @param {*} indexCode 
        * @param {*} callback 
     */
    get(indexCode, callback) {
        if (indexCode) {
            let url = this.BASE_URL + "/get?indexCode=" + indexCode;
            request.get(url, callback);
        }
    },
    /**
      * 根据indexCode删除常见缺陷信息
      * @param {*} indexCode 
      * @param {*} callback 
   */
    delete(indexCode, callback) {
        if (indexCode) {
            let url = this.BASE_URL + "/delete?indexCode=" + indexCode;
            request.delete(url, callback);
        }
    },

    /**
      * 根据deleteDefectCodes加载常见缺陷信息
      * @param {*} deleteDefectCodes 
      * @param {*} callback 
   
    deletes(deleteDefectCodes, callback) {
        let url = this.BASE_URL + "/deletes?t=1";
        if (deleteDefectCodes != undefined) {
            deleteDefectCodes.forEach(defectCode => {
                url += "&defectCode=" + defectCode;
            });
            request.delete(url, callback);
        }
    },*/

    //请求的API
    list(page = 1, limit = 10, index, callback) {
        //url
        let url = this.BASE_URL + "/list?page=" + page + "&limit=" + limit;
        if (index) {
            if (index.indexCode) {
                url += "&indexCode=" + index.indexCode;
            }
            if (index.indexName) {
                url += "&indexName=" + index.indexName;
            }

            if (index.indexType) {
                url += "&indexType=" + index.indexType;
            }
        }
        //发送请求
        request.get(url, callback);

    }
}

//将defectRequest暴漏出去
export default indexRequest;